using System.Collections;

using LocalBoard.Domain;
using LocalBoard.Persistence;

namespace LocalBoard.Controller
{
	[NUnit.Framework.TestFixture]
	public class TestPollMessageController
	{
		[NUnit.Framework.SetUp]
		public void setUp()
		{
			_accessorFactory = new MockAccessorFactory();
			_user = _accessorFactory.GetProfileAccessor().Get(_accessorFactory.GetProfileAccessor().Insert(new Profile("userId 1","userName 1","password","test@test.com")));
			int clubId = _accessorFactory.GetClubAccessor().Insert(new Club("testClub", "test club", 1, "","", null));
			ClubController clubController = new ClubController(clubId, _accessorFactory);
			int boardId = clubController.CreateBoard(new Board("test board", null, 0, 0, clubController.ClubId, BoardViewType.List));
			BoardController boardController = new BoardController(boardId,clubController, 1, _accessorFactory); 
			_messageId = boardController.PostPollMessage("subject 1","body 1",_user,0);    
			_unitUnderTest = new PollMessageController(_messageId, _accessorFactory); 
		}
		[NUnit.Framework.TearDown]
		public void tearDown()
		{
			MockAccessorFactory.ResetDataTable();

			_unitUnderTest = null;
		}
		[NUnit.Framework.Test]
		public void testAddPollItem()
		{
			_unitUnderTest.AddPollItem(new PollItem("test poll1"));
             
			PollMessage message = _accessorFactory.GetMessageAccessor().Get(_messageId) as PollMessage;
			NUnit.Framework.Assertion.AssertNotNull(message);
			foreach(PollItem pollItem in message.PollItems)
			{
				if(pollItem.Name == "test poll1")
				{
					NUnit.Framework.Assertion.Assert(true);
					return;
				}
			}
			NUnit.Framework.Assertion.Fail("Can't find posted pollitem");
		}
		[NUnit.Framework.Test]
		public void testDeletePollItem()
		{
			int pollItemId = _unitUnderTest.AddPollItem(new PollItem("test poll2"));
			_unitUnderTest.DeletePollItem(   pollItemId  );
             
			PollMessage message = _accessorFactory.GetMessageAccessor().Get(_messageId) as PollMessage;
			foreach(PollItem pollItem in message.PollItems)
			{
				if(pollItem.Name == "test poll2")
				{
					NUnit.Framework.Assertion.Fail("Can't delete posted pollitem");
					return;
				}
			}
			NUnit.Framework.Assertion.Assert(true);
		}
		[NUnit.Framework.Test]
		public void testPostPoll()
		{
			int pollItemId = _unitUnderTest.AddPollItem(new PollItem("test poll3"));
			_unitUnderTest.PostPoll(_user.Id, pollItemId);

			PollMessage message = _accessorFactory.GetMessageAccessor().Get(_messageId) as PollMessage;
			NUnit.Framework.Assertion.Assert(message.DidUserVote(_user.Id));
		}

		private int _messageId;
		private Profile _user = null;
		private MockAccessorFactory _accessorFactory = null;
		private PollMessageController _unitUnderTest;
	}
}
